package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    //用户缓存服务 redis
    @Reference
    private IUserInfoRedisService userInfoRedisService;

    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    //验证码手机是否已存在
    @Override
    public boolean checkPhone(String phone) {

        int count = userInfoMapper.selectCountByPhone(phone);

        return count > 0;
    }

    //发送验证码
    @Override
    public void sendVerifyCode(String phone) {
        //判断信息发送是否太频繁
        String ref = userInfoRedisService.getMessageTime();
        if(ref != null){
            throw new LogicException("你发送信息太频繁了,请稍后再试");
        }

        //随机验证码
        String code = UUID.randomUUID().toString().replace("-", "").substring(0, 4);
        //拼接验证码
        StringBuilder sb = new StringBuilder(100);
        sb.append("你的验证码是:").append(code).append("请在").append(Consts.VERIFY_CODE_VAI_TIME).append("内使用");
        System.out.println(sb);

        //把验证码和phone存储到redis中
        userInfoRedisService.setVerifyCode(phone, code);
    }

    //完成注册
    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //1.判断参数是否为空
        AssertUtil.hasLength(phone, "手机号码不能为空");
        AssertUtil.hasLength(nickname, "昵称不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "二次密码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");

        //2.判断两次输入密码是否一致
        AssertUtil.hasEquals(password, rpassword, "两次密码不一致");

        //3.判断手机号码格式是否正确
        String regExp = "^[1]([3][0-9]{1}|59|58|88|89)[0-9]{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(phone);
        if(!m.find()){
            throw new LogicException("手机号码格式不正确");
        }

        //4.判断手机号码是否唯一
        int count = userInfoMapper.selectCountByPhone(phone);
        if (count > 0) {
            throw new LogicException("手机号已被注册");
        }

        //5.判断验证码是否正确,是否过期
        String redisCode = userInfoRedisService.getVerifyCode(phone);
        if (redisCode == null) {
            throw new LogicException("验证码已失效");
        }
        if (!verifyCode.equals(redisCode)) {
            throw new LogicException("验证码不正确");
        }

        //6.注册
        UserInfo userInfo = new UserInfo();
        userInfo.setLevel(1); //设置等级为1
        userInfo.setState(UserInfo.STATE_NORMAL); //设置状态为正常
        userInfo.setHeadImgUrl("/images/default.jpg"); //设置默认头像
        userInfo.setNickname(nickname);
        userInfo.setPassword(password);
        userInfo.setPhone(phone);
        userInfoMapper.insert(userInfo);
    }

    //用户登录,共享数据等操作...
    @Override
    public String userLogin(String username, String password) throws LogicException {
        //非空判断
        AssertUtil.hasLength(username, "账号不能为空");
        AssertUtil.hasLength(password, "密码不能为空");

        //匹配账号密码是否一致
        UserInfo user = userInfoMapper.selectUserByUsernameAndPassword(username,password);
        if(user == null){
            throw new LogicException("账号或密码错误!");
        }

        //创建token
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        //存放到redis中,token为key,user为value
        userInfoRedisService.setToken(token,user);

        return token;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }

}
